package com.xxh.test;

import com.xxh.dataaccess.dao.*;
import com.xxh.dataaccess.dao.recall.MovieSimarRecDao;
import com.xxh.dataaccess.dao.recall.VideoSimarRecDao;
import com.xxh.dataaccess.dataAccessApp;
import com.xxh.dataaccess.dto.CommentDto;
import com.xxh.dataaccess.dto.ContactMessage;
import com.xxh.dataaccess.dto.Contacts;
import com.xxh.dataaccess.dto.MovieDto;
import com.xxh.dataaccess.dto.message.ReplyMessageDto;
import com.xxh.dataaccess.entity.*;
import com.xxh.dataaccess.page.MongoPageHelper;
import com.xxh.dataaccess.page.PageResult;
import com.xxh.dataaccess.service.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = dataAccessApp.class)
public class dataAccess {

    @Resource
    UserDao userDao;

    @Autowired
    VideoDao VideoDao;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    MongoPageHelper pageHelper;

    @Autowired
    FriendService friendService;


    @Resource
    MovieService movieService;

    @Resource
    ChatMessageDao chatMessageDao;

    @Autowired
    CommentDao commentDao;

    @Resource
    CommentService commentService;

    @Resource
    MovieSimarRecDao movieSimarRecDao;

    @Autowired
    UserMessageService userMessageService;


    @Autowired
    ProudService proudService;

    @Autowired
    VideoSimarRecDao videoSimarRecDao;


    @Test
    public void testDb(){

//        List<User> users = userDao.selectList(null);
//        System.out.println(users.size());

        User user = userDao.findAllByUsername("xiong");
        System.out.println(user);
    }

    @Test
    public void tesVideo(){
        Pageable pageable = PageRequest.of(1,12);

        Query query = new Query();
        Criteria criteria =new Criteria();

        int pageNum=1;
        int pageSize=20;
        long count = mongoTemplate.count(query, Video.class);
        int pageTotal=(int) (count%pageSize==0?count/pageSize:count/pageSize+1); //总页数

        int offset=(pageNum-1)*pageSize;
        query.skip(offset).limit(pageSize); // 分页逻辑
        List<Video> vs = mongoTemplate.find(query, Video.class);

        System.out.println(vs);
    }

    @Test
    public void  testQuery(){
        Pageable pageable = PageRequest.of(1,12);

        Page<Video> all = VideoDao.findAll(pageable);
        List<Video> content = all.getContent();
        //拿到具体结果
        System.out.println(content.size());
        //总页数
        System.out.println("total page:"+all.getTotalPages());
        //添加排序条件 升降、根据字段
        VideoDao.findAll(PageRequest.of(1,12,Sort.by(Sort.Direction.ASC,"view")));

    }


    //强烈推荐使用
    @Test
    public void MulitQuery(){
        //复杂查询,通过Criteria完成
        Query query = new Query();
        query.addCriteria(Criteria.where("tname").is("翻唱"));
        //指定排序条件 Desc是降序哦
        query.with(Sort.by(Sort.Direction.DESC,"view"));
        //指定分页参数
        Pageable pageable=PageRequest.of(0,10);
        query.with(pageable);

        List<Video> videos = mongoTemplate.find(query, Video.class);

        System.out.println(videos);
    }

    //测试分页
    @Test
    public void TestPageHelper(){
        Query query = new Query();
        query.addCriteria(Criteria.where("tname").is("翻唱"));
        PageResult<Video> result = pageHelper.pageQuery(query, Video.class, 10, 1);
        System.out.println(result);

    }

    //用户收藏
    @Test
    public void  testUserFav(){

    }
    //查询好友
    @Test
    public void getFriend(){
        List<Integer> fids = friendService.getFriendID(12);
    }

    //查询最近联系人

    @Test
    public  void getContacts(){
        List<Contacts> contacts = chatMessageDao.getContactsByUid(12);
        System.out.println(contacts);
        List<ContactMessage> list = chatMessageDao.pullMessage(12, 14);
        System.out.println(list);
    }

    //分页查询电影
    @Test
    public  void listMovies(){
        PageResult<MovieDto> movieDtoPageResult = movieService.PagingMovies(1, 20);
        System.out.println(movieDtoPageResult);

        List<String> allMovieTypes = movieService.getAllMovieTypes();
        System.out.println(allMovieTypes);
    }
    //查询评论


    @Test
    public void  commentQuery(){
        List<CommentDto> data = commentService.getComments("BV1gN411o7cv");
        System.out.println(data.size());

        List<ReplyMessageDto> message = userMessageService.getUserReplyMessage(12);
        System.out.println(message);
    }


    @Test
    public void  getSimilarMovie(){

//        MovieSimarRec rec = movieSimarRecDao.findByMid(1);
//        System.out.println(rec);

//        List<Movie> movie = movieService.getSimilarMovie(1);
//        System.out.println(movie);

        List<Movie> movies = movieService.getMovieByUid(23719);
        System.out.println(movies);
    }

    @Test
    public void addProudVideos(){
//        proudService.setProudVideos("bv5523eca6c7b5428fa6490f91d7fabf63", 12L);
        proudService.setProudVideos("bv432e1ba9aacc4988b2e82fabea3e6256", 12L);
        System.out.println(proudService.getProudVideos(12L));
    }

    @Test
    public void  testVideoRec(){
        VideoSimarRec rec = videoSimarRecDao.getVideoSimarRecByBv("BV1gN411o7cv");

        List<Video> videos = VideoDao.findByBvIn(rec.getRec_list());
        System.out.println(videos);
    }



}
